#include "common.h"

static int msgid1;
static int msgid2;

void getKey()
{
	if((msgid1 = msgget(key1,0)) == -1)
	{
		perror("queue 1 fail!") , exit(-1);
	}
	if((msgid2 = msgget(key2,0)) == -1)
	{
		perror("queue 2 fail!") , exit(-1);
	}
}

void print()
{
	printf(".");
}

void load()
{
	signal(SIGALRM,print);
	struct itimerval timer;
	timer.it_interval.tv_sec = 1;
	timer.it_interval.tv_usec = 0;
	timer.it_value.tv_sec = 0;
	timer.it_value.tv_usec = 0;
	setitimer(ITIMER_REAL,&timer,0);	
}

void establishAccount()
{
	struct Msg m1;
	m1.mtype = M_OPEN;
	printf("name : ");
	scanf("%s",m1.acc[0].name);
	printf("pwd : ");
	scanf("%s",m1.acc[0].pwd);
	printf("balance : ");
	scanf("%lf",&m1.acc[0].balance);

	getKey();
	
	//send messages
	int res = msgsnd(msgid1 , &m1 , sizeof(m1) , 0);
	if(res == -1)
		perror("sending message to server failed!") , exit(-1);
	//receive messages
	res = msgrcv(msgid2 , &m1  , sizeof(m1) , 0 , 0);
	if(res == -1)
		perror("receving message from server failed!") , exit(-1);
	if(m1.mtype == M_SUCCESS)
		printf("creat account successfully!\n");
	else
		printf("creating account failed!\n");
}

void closeAccount()
{
	//1.input the name of account closed
	struct Msg m1;
	m1.mtype = M_DESTORY;
	printf("input the num of account you want to close : ");
	scanf("%d",&m1.acc[0].num);
	//2.check the 2 queue
	getKey();	
	//3.send the message to server
	int res = msgsnd(msgid1 , &m1 , sizeof(m1) , 0);
	if(res == -1)
		perror("sending message to server failed!") , exit(-1);
	//receive messages
	res = msgrcv(msgid2 , &m1  , sizeof(m1) , 0 , 0);
	if(res == -1)
		perror("receving message from server failed!") , exit(-1);
	if(m1.mtype == M_SUCCESS)
		printf("close account successfully!\n");
	else
		printf("fail to close account!\n");
}

void saveMoney()
{
	//1.input the name of account closed
	struct Msg m1;
	m1.mtype = M_SAVE;
	printf("input account of id you want to save : ");
	scanf("%d",&m1.acc[0].num);
	printf("input the amount of money want to save : ");
	scanf("%lf",&m1.acc[0].balance);

	//2.check the 2 queue
	getKey();

	//3.send the message to server
	int res = msgsnd(msgid1 , &m1 , sizeof(m1) , 0);
	if(res == -1)
		perror("sending message to server failed!") , exit(-1);
	//receive messages
	res = msgrcv(msgid2 , &m1  , sizeof(m1) , 0 , 0);
	if(res == -1)
		perror("receving message from server failed!") , exit(-1);
	if(m1.mtype == M_SUCCESS)
		printf("save successfully!\n");
	else
		printf("fail to save money!\n");
}

void withdrawAccount()
{
	//1.input the name of account closed
	struct Msg m1;
	m1.mtype = M_WITHDRAW;
	printf("input account of id you want to withdraw : ");
	scanf("%d",&m1.acc[0].num);
	printf("password :");
	scanf("%s",m1.acc[0].pwd);
	printf("input the amount of money you want to withdraw : ");
	scanf("%lf",&m1.acc[0].balance);

	//2.check the 2 queue
	getKey();

	//3.send the message to server
	int res = msgsnd(msgid1 , &m1 , sizeof(m1) , 0);
	if(res == -1)
		perror("sending message to server failed!") , exit(-1);
	//receive messages
	res = msgrcv(msgid2 , &m1  , sizeof(m1) , 0 , 0);
	if(res == -1)
		perror("receving message from server failed!") , exit(-1);
	if(m1.mtype == M_SUCCESS)
		printf("withdraw successfully!\n");
	else
		printf("fail to withdraw money!\n");
}

void queryAccount()
{
	//1.input the name of account closed
	struct Msg m1;
	m1.mtype = M_QUERY;
	printf("input account of id you want to query : ");
	scanf("%d",&m1.acc[0].num);
	printf("password :");
	scanf("%s",m1.acc[0].pwd);
	//2.check the 2 queue
	getKey();	
	//3.send the message to server
	int res = msgsnd(msgid1 , &m1 , sizeof(m1) , 0);
	if(res == -1)
		perror("sending message to server failed!") , exit(-1);
	//receive messages
	res = msgrcv(msgid2 , &m1  , sizeof(m1) , 0 , 0);
	if(res == -1)
		perror("receving message from server failed!") , exit(-1);
	if(m1.mtype == M_SUCCESS)
	{
		printf("------------------\n");
		printf("name :%s\n" ,m1.acc[0].name);
		printf("balance :%.2f\n" ,m1.acc[0].balance);
		printf("------------------\n");
	}		
	else
		printf("fail to query!\n");
}

void transferAccount()
{
	//1.input the name of account closed
	struct Msg m1;
	m1.mtype = M_TRANSFER;
	
	printf("input your account of id : ");
	scanf("%d",&m1.acc[0].num);
	printf("your password :");
	scanf("%s",m1.acc[0].pwd);
	printf("input account of id you want to transfer : ");
	scanf("%d",&m1.acc[1].num);
	printf("input the amount of money you want to transfer : ");
	scanf("%lf",&m1.acc[0].balance);

	//2.check the 2 queue
	getKey();	
	//3.send the message to server
	int res = msgsnd(msgid1 , &m1 , sizeof(m1) , 0);
	if(res == -1)
		perror("sending message to server failed!") , exit(-1);
	//receive messages
	res = msgrcv(msgid2 , &m1  , sizeof(m1) , 0 , 0);
	if(res == -1)
		perror("receving message from server failed!") , exit(-1);
	if(m1.mtype == M_SUCCESS)
	{
		printf("transfer successfully\n");
	}		
	else
		printf("fail to query!\n");
}

//client
int main()
{
	int num ;
	load();
	while(1)
	{
		printf("please input the number 1-6 :\n");
		printf("1. create an acount \n");
		printf("2. close an acount \n");
		printf("3. save \n");
		printf("4. withdraw\n");
		printf("5. query account \n");
		printf("6. transfer account \n");
		printf("0. quit the process ");
		scanf("%d",&num);
                scanf( "%*[^\n]");
                scanf( "%*c");	
		switch(num)
		{
			case 1:
				establishAccount();
				break;
			case 2:
				closeAccount();
				break;
			case 3:
				saveMoney();
				break;
			case 4:
				withdrawAccount();
				break;
			case 5:
				queryAccount();
				break;
			case 6:
				transferAccount();
				break;
			case 0:
				printf("exit\n");
				return 0;
			default:
				printf("sorry , it's wrong figure , please try again\n");
				//alarm(2);
				break;
		}	
	}
	exit(0);
}


